<?php declare(strict_types=1);
/**
 * @author      xianganyall <xianganyall@gmail.com>
 * @copyright   2023-2025 owner
 **/

namespace Srv\Libs\Plugins\MapGeo;

class Polygon
{
    private bool $isInit        = false;
    private float $minX         = 0.0;      // 矩形左下角的x坐标
    private float $minY         = 0.0;      // 矩形左下角的y坐标
    private float $maxX         = 0.0;      // 矩形右上角的x坐标
    private float $maxY         = 0.0;      // 矩形右上角的y坐标
    private array $points       = [];       // 点列表

    /**
     * @param array $points
     * __construct
     */
    public function __construct(array $points)
    {
        foreach($points as $point){
            if(!($point instanceof Point)) continue;
            if(!$this->isInit){
                $this->isInit       = true;
                $this->minX         = $point->lng;
                $this->minY         = $point->lat;
                $this->maxX         = $point->lng;
                $this->maxY         = $point->lat;
            }else{
                $this->minX         = min($point->lng, $this->minX);
                $this->minY         = min($point->lat, $this->minY);
                $this->maxX         = max($point->lng, $this->maxX);
                $this->maxY         = max($point->lat, $this->maxY);
            }
            $this->points[]         = $point;
        }
    }

    /**
     * @return Bounds
     * 返回覆盖物的地理区域范围
     */
    public function getBounds():Bounds
    {
        return new Bounds(new Point($this->minX, $this->minY), new Point($this->maxX, $this->maxY));
    }

    /**
     * @return array
     * 返回多边型的点数组
     */
    public function getPath():array
    {
        return $this->points;
    }
}